/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JProductLinkPanel.java
 *
 * Created on 2009-11-3, 13:29:29
 */

package cn.ekuma.epos.qkorder;

import cn.ekuma.epos.qkorder.ordermanager.OrderManager;
import cn.ekuma.epos.qkorder.link.JOrderLinkPanel;
import cn.ekuma.epos.datalogic.I_DataLogicCustomers;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.qkorder.link.JOperationProductCategoryPanel;
import cn.ekuma.epos.qkorder.link.JOrderDetailPanel;
import cn.ekuma.epos.qkorder.link.JOrderPaymentsPanel;
import cn.ekuma.epos.qkorder.link.JOrderStatesPanel;
import cn.ekuma.epos.qkorder.link.JProductBomIncludeStorePanel;
import cn.ekuma.epos.qkorder.link.JProductCodeSimilarPanel;
import cn.ekuma.epos.qkorder.link.JProductIncludeBomStorePanel;
import cn.ekuma.epos.qkorder.link.JProductShardPanel;
import cn.ekuma.epos.qkorder.link.JSimilarProductStorePanel;

import com.openbravo.bean.AppUser;
import com.openbravo.data.loader.key.KeyBuilder;

import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.base.AppView;
import com.openbravo.pos.panels.link.AbstractProductLinkTablePanel;
import com.openbravo.pos.panels.link.JBomProductForIncludePanel;
import com.openbravo.pos.panels.link.JCustomerStorePanel;
import com.openbravo.pos.panels.link.JProductLotNoStorePanel;
import com.openbravo.pos.panels.link.JProductStorePanel;
import com.openbravo.pos.panels.link.JQuickProductPanel;
import com.openbravo.pos.panels.link.JSeachProductPanel;

import java.awt.Component;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


/**
 *label.orderstatus
 * @author Administrator
 */
@SuppressWarnings("serial")
public class JLinkContainerPanel extends javax.swing.JPanel{
     boolean hide;
    private String productID;
    private final I_DataLogicERP dlSales;
    private final I_DataLogicCustomers dlCustomers;
    private OrderManager orderManager;
    private  JPanelOrder jPanelOrder;
    AppView app;
    JOrderDetailPanel detailPanel;
    JLinkContainerTotalBar totalBar;
	private JProductShardPanel jProductShardPanel;

    public JLinkContainerPanel(JPanelOrder orderPanel) {
        initComponents();
        this.dlSales=orderPanel.getDlSales();
        this.dlCustomers=orderPanel.getDlCustomers();
        hide=false;
        productID=null;
        jPanelOrder=orderPanel;
        app=orderPanel.m_App;
        orderManager=orderPanel.orderManager;
        totalBar=new JLinkContainerTotalBar(this, jPanelOrder.getDlReceipts());
        jTabbedPane1.addChangeListener(new ChangeListener(){
            @Override
            public void stateChanged(ChangeEvent e) {
                fireProductChanged(productID,null);
            }
        });
        detailPanel=new JOrderDetailPanel(this);
        orderManager.addOrderChangedListener(detailPanel);
        jTabbedPane1.addTab(AppLocal.getIntString("label.orderstatus"), detailPanel);
        
        if(!app.isCustomerView()){
        	JOrderLinkPanel orderLinkPanel=new JOrderLinkPanel(app, dlSales, jPanelOrder.getDlSystem(), dlCustomers);
            orderManager.addOrderChangedListener(orderLinkPanel);
            jTabbedPane1.addTab(AppLocal.getIntString("label.OrderLinkPanel.name"), orderLinkPanel);
        }
        
        JProductStorePanel productStorePanel=new JProductStorePanel(dlSales);
        jTabbedPane1.addTab(AppLocal.getIntString("label.prodstock"), productStorePanel);
        jTabbedPane1.addTab(AppLocal.getIntString("label.SimilarProductStore"), new JSimilarProductStorePanel(dlSales));
        
        JOrderStatesPanel orderStatesPanel=new JOrderStatesPanel((AppUser) jPanelOrder.m_App.getAppUserView().getUser(),orderManager.getStateMachine());
        orderManager.addOrderChangedListener(orderStatesPanel);
        jTabbedPane1.addTab(AppLocal.getIntString("label.orderStates"),orderStatesPanel);
        
        JOrderPaymentsPanel orderPaymentPanel=new JOrderPaymentsPanel();
        orderManager.addOrderChangedListener(orderPaymentPanel);
        jTabbedPane1.addTab(AppLocal.getIntString("Label.Payment"),orderPaymentPanel);
        
        JProductCodeSimilarPanel jProductCodeSimilarPanel=new JProductCodeSimilarPanel(dlSales);
        jTabbedPane1.addTab(AppLocal.getIntString("label.SimilarCodeProduct"),jProductCodeSimilarPanel);
        jProductCodeSimilarPanel.addProductChangedListener(orderPanel);
        
              
        
        JProductBomIncludeStorePanel productBomIncludeStorePanel=new JProductBomIncludeStorePanel(dlSales);
        jTabbedPane1.addTab(AppLocal.getIntString("label.prodBomIncludeStock"), productBomIncludeStorePanel);  
        
        JProductIncludeBomStorePanel productIncludeBomStorePanel=new JProductIncludeBomStorePanel(dlSales);
        jTabbedPane1.addTab(AppLocal.getIntString("label.prodIncludeBomStock"), productIncludeBomStorePanel);
        
        JProductLotNoStorePanel productLotNoStorePanel=new JProductLotNoStorePanel(dlSales);
        jTabbedPane1.addTab(AppLocal.getIntString("label.lotNoStore"), productLotNoStorePanel);
        
        JBomProductForIncludePanel jBomProductForIncludePanel=new JBomProductForIncludePanel(dlSales);
        jTabbedPane1.addTab(AppLocal.getIntString("label.prodIncludeBom"),jBomProductForIncludePanel);
        jBomProductForIncludePanel.addProductChangedListener(orderPanel);
        
        JCustomerStorePanel customerNoStorePanel=new JCustomerStorePanel(dlSales);
        jTabbedPane1.addTab(AppLocal.getIntString("label.customerStore"), customerNoStorePanel);
        
        
        JSeachProductPanel seachProductPanel=new JSeachProductPanel(dlSales);
        jTabbedPane1.addTab(AppLocal.getIntString("label.search"), seachProductPanel);
        seachProductPanel.addProductChangedListener(orderPanel); 
        
        if(!app.isCustomerView()){
        	 jTabbedPane1.addTab(AppLocal.getIntString("label.operationProductCategory"), new JOperationProductCategoryPanel(dlSales));

            jProductShardPanel=new JProductShardPanel(dlSales);
            jTabbedPane1.addTab(AppLocal.getIntString("Menu.SharedProducts"), jProductShardPanel);
            jProductShardPanel.addProductChangedListener(orderPanel);
        	
        	JQuickProductPanel quickProductPanel=new JQuickProductPanel(dlSales);
            jTabbedPane1.addTab(AppLocal.getIntString("label.quickProduct"), quickProductPanel);
            quickProductPanel.addProductChangedListener(orderPanel); 
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();

        setMaximumSize(new java.awt.Dimension(2147483647, 350));
        setPreferredSize(new java.awt.Dimension(800, 180));
        setLayout(new java.awt.BorderLayout());

        jTabbedPane1.setFocusable(false);
        jTabbedPane1.setRequestFocusEnabled(false);
        add(jTabbedPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>


    // Variables declaration - do not modify
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration

    void fireProductChanged(String productID,Object o) {
        this.productID=productID;
        if(! hide){
           Component selectTab=jTabbedPane1.getSelectedComponent();
           if(selectTab!=null&&selectTab instanceof AbstractProductLinkTablePanel){
        	   AbstractProductLinkTablePanel linstener=(AbstractProductLinkTablePanel)selectTab;
               linstener.baseBeanChanged(KeyBuilder.getKey(productID),null);
           }
        }
    }

    public Component getToolbar() {
        return totalBar;
    }

    public void resetComponent() {
        if(detailPanel.isVisible())
        	detailPanel.printOrderState();
        totalBar.resetComponent();
    }

    public void printPartialTotals() {
        if(hide)
        	totalBar.printParialTotals();
        else
        	detailPanel.printPartialTotals(); 
    }
    

    public Component getViewToolbar() {
    	return new JLinkContainerTotalBar(this, jPanelOrder.getDlReceipts());
    }
    
    public void addPanel(String title,Component pane){
    	jTabbedPane1.add(title,pane);
    }
    
    public Component getSelectedComponent(){
    	return jTabbedPane1.getSelectedComponent();
    }

	public String getProductID() {
		return productID;
	}

	public void setProductID(String productID) {
		this.productID = productID;
	}

	public OrderManager getOrderManager() {
		return orderManager;
	}

	public void setOrderManager(OrderManager orderManager) {
		this.orderManager = orderManager;
	}

	public JPanelOrder getjPanelOrder() {
		return jPanelOrder;
	}

	public void setjPanelOrder(JPanelOrder jPanelOrder) {
		this.jPanelOrder = jPanelOrder;
	}

	public I_DataLogicERP getDlSales() {
		return dlSales;
	}

	public I_DataLogicCustomers getDlCustomers() {
		return dlCustomers;
	}

	public AppView getApp() {
		return app;
	}

	public void setApp(AppView app) {
		this.app = app;
	}

	public void removePanel(Component selectTab) {
		jTabbedPane1.remove(selectTab);
	}
    
    
}
